﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Imaging

Imports Bytescout.Scan
Imports System.IO

Public Class frmScan

    Private _scan As Scan ' define scan object

    Private Sub frmScan_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ' Create new Scan object
        _scan = New Scan()
        ' Handle the event notifying the image trasfer is finished.
        ' _scan.TransferFinished += scan_TransferFinished()
        AddHandler _scan.TransferFinished, AddressOf scan_TransferFinished
    End Sub
   


    ' this event captures scanned images and fills images into tabs
    ' you can also iterate through scannedImages array to get each image as an Image object instead
    Private Sub scan_TransferFinished(ByVal sender As Scan, ByVal scannedImages As ArrayList)
        FillTabsWithImages(scannedImages)
    End Sub

    ' this function puts retrieved images objects into tabs on the form
    Private Sub FillTabsWithImages(ByVal images As ArrayList)
        tabControl1.TabPages.Clear()
        For i As Integer = 0 To images.Count - 1

            Dim page As New TabPage()

            Dim pb As New PictureBox()
            pb.Dock = DockStyle.Fill
            pb.SizeMode = PictureBoxSizeMode.Zoom

            If TypeOf images(i) Is Image Then
                page.Text = "Image " + (i + 1).ToString()
                pb.Image = DirectCast(images(i), Image)
            Else
                page.Text = DirectCast(images(i), String)
                pb.Image = Image.FromFile(DirectCast(images(i), String))
            End If

            page.Controls.Add(pb)

            tabControl1.TabPages.Add(page)
        Next
    End Sub

    ' acquire images into files automatically saved into the given folder with given image format and filename
    Private Sub btnScanToFilesAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScanToFilesAsync.Click

        ' set SDK to save images into files
        _scan.SaveTo = SaveTo.File
        ' scan.FileNamingTemplate = "ScannedImage_{0:D2}"; // you can set output filename template
        ' set output format as JPG image format
        _scan.OutputFormat = OutputFormat.JPEG
        AddHandler _scan.TransferFinished, AddressOf scan_TransferFinished

        ' you can set output folder as well, by default output folder is the same where executable .exe is running            
        'scan.OutputFolder = Path.GetTempPath();


        ' assign the event to indicate when image transfer is finished

        ' acquire images
        _scan.AcquireImagesAsync(Me.Handle)
    End Sub

    ' displays ready-to-use options dialog for the scan SDK

    Private Sub btnOptions_Click(sender As System.Object, e As System.EventArgs) Handles btnOptions.Click
        ' _scan.ShowOptionsDialog(New Scan())
        Scan.ShowOptionsDialog(New Scan())

    End Sub

    Private Sub btnSelectDevice_Click(sender As System.Object, e As System.EventArgs) Handles btnSelectDevice.Click
        ' Display TWAIN device selection dialog
        '_scan.SelectDevice(Me.Handle)
        _scan.SelectDevice(Me.IsHandleCreated)
    End Sub
    ' Starts Asynchronous scan into images. When scan is finished then scan_TransferFinished event is called and array with images objects is passed into this event

    Private Sub btnScanToImagesAsync_Click(sender As System.Object, e As System.EventArgs) Handles btnScanToImagesAsync.Click

        ' set Scan object to save images into images array (will be passed as a paramtere in scan_TransferFinished event
        _scan.SaveTo = SaveTo.ImageObject
        AddHandler _scan.TransferFinished, AddressOf scan_TransferFinished

        ' set function for scan finished event to indicate when image transfer is finished
        ' see scan_TransferFinished event below
        ' acquire images
        _scan.AcquireImagesAsync(Me.Handle)
    End Sub

End Class



